<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>MySQL 重置密码 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="MySQL 重置密码 假设我们使用的是 root 账户  跳过 MySQL 的密码认证过程 1vim &#x2F;etc&#x2F;my.cnf  	# Terminal下执行(Windows下修改的是 my.ini)  在文档中搜索 mysqld 定位到含有 [mysqld] 的文本段. 1&#x2F;mysqld 					# 在 vim 命令状态下直接输入该命令可搜索文本内容  在 [mysqld] 后面任意一行添加 s">
<meta property="og:type" content="article">
<meta property="og:title" content="MySQL 重置密码">
<meta property="og:url" content="https://lummer.gitee.io/hexo/2021/01/16/MySQL%20%E9%87%8D%E7%BD%AE%E5%AF%86%E7%A0%81/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="MySQL 重置密码 假设我们使用的是 root 账户  跳过 MySQL 的密码认证过程 1vim &#x2F;etc&#x2F;my.cnf  	# Terminal下执行(Windows下修改的是 my.ini)  在文档中搜索 mysqld 定位到含有 [mysqld] 的文本段. 1&#x2F;mysqld 					# 在 vim 命令状态下直接输入该命令可搜索文本内容  在 [mysqld] 后面任意一行添加 s">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/Lummer-Li/image-bucket/img/20210116145218.png">
<meta property="article:published_time" content="2021-01-16T07:27:37.000Z">
<meta property="article:modified_time" content="2021-09-20T10:12:33.411Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.jsdelivr.net/gh/Lummer-Li/image-bucket/img/20210116145218.png">
  
    <link rel="alternate" href="/hexo/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/hexo/css/style.css">

<meta name="generator" content="Hexo 5.4.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/hexo/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/hexo/">Home</a>
        
          <a class="main-nav-link" href="/hexo/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/hexo/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://lummer.gitee.io/hexo"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-MySQL 重置密码" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/hexo/2021/01/16/MySQL%20%E9%87%8D%E7%BD%AE%E5%AF%86%E7%A0%81/" class="article-date">
  <time datetime="2021-01-16T07:27:37.000Z" itemprop="datePublished">2021-01-16</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      MySQL 重置密码
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h3 id="MySQL-重置密码"><a href="#MySQL-重置密码" class="headerlink" title="MySQL 重置密码"></a>MySQL 重置密码</h3><hr>
<p>假设我们使用的是 root 账户</p>
<ol>
<li><p>跳过 MySQL 的密码认证过程</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim /etc/my.cnf  	# Terminal下执行(Windows下修改的是 my.ini)</span><br></pre></td></tr></table></figure>

<p>在文档中搜索 <code>mysqld</code> 定位到含有 <code>[mysqld]</code> 的文本段.</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">/mysqld 					# 在 vim 命令状态下直接输入该命令可搜索文本内容</span><br></pre></td></tr></table></figure>

<p>在 <code>[mysqld]</code> 后面任意一行添加 <code>skip-grant-tables</code> 用来跳过密码验证的过程.</p>
<p><img src="https://cdn.jsdelivr.net/gh/Lummer-Li/image-bucket/img/20210116145218.png" alt="image-20210116145218258"></p>
<p>保存文档并退出.</p>
</li>
<li><p>重启 MySQL</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">/etc/init.d/mysql restart		# Terminal下执行(部分用户可能需要使用/etc/init.d/mysqld restart)</span><br></pre></td></tr></table></figure></li>
<li><p>重启之后输入 mysql 进入 mysql</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mysql		# Terminal下执行</span><br></pre></td></tr></table></figure></li>
<li><p>修改 root 密码</p>
<ul>
<li><p>Terminal 终端下键入 <code>mysql -u root -p </code> 命令, 回车(如果 Mac 没有设置环境路径, 键入 <code>/usr/local/MySQL/bin/mysql -u root -p</code> 命令, 回车即可)</p>
</li>
<li><p>当需要输入密码时, 直接按回车键即可</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; update user set password=password(“新密码”) where user=&quot;root&quot;;</span><br><span class="line">mysql&gt; flush privileges;</span><br><span class="line">mysql&gt; quit;</span><br><span class="line"># 注意事项: 如果执行该步骤是出现 ERROR 1290(HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement 错误. 则执行 flush privileges 命令, 在执行命令即可</span><br></pre></td></tr></table></figure></li>
<li><p>完成密码重置</p>
</li>
</ul>
</li>
<li><p>编辑 my.cnf 删除刚才添加的内容, 重启 MySQL 即可    </p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"># 注意事项: 修改 mysql 密码是报错: ERROR 1064(42000): You have an error in your SQL syntax; check the manual that corres, 则有以下两种解决方法:</span><br><span class="line">mysql&gt; SET PASSWORD = &#x27;123456&#x27;; </span><br><span class="line">mysql&gt; ALTER USER &#x27;root&#x27;@&#x27;localhost&#x27; IDENTIFIED BY &#x27;123456&#x27;;</span><br><span class="line"># 以上两种方法使用其中一种即可</span><br></pre></td></tr></table></figure></li>
</ol>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://lummer.gitee.io/hexo/2021/01/16/MySQL%20%E9%87%8D%E7%BD%AE%E5%AF%86%E7%A0%81/" data-id="cktshq5a50008du9lh4qu3uvm" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/hexo/2021/01/26/JDBC/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          JDBC
        
      </div>
    </a>
  
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2021/09/">September 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2021/08/">August 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2021/07/">July 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2021/03/">March 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2021/02/">February 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2021/01/">January 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/hexo/2021/09/15/hello-world/">Hello World</a>
          </li>
        
          <li>
            <a href="/hexo/2021/08/20/Mac%20%E5%85%A8%E5%B1%8F%E6%97%B6%E8%87%AA%E5%8A%A8%E9%9A%90%E8%97%8F%E7%A8%8B%E5%BA%8F%E5%9D%9E/">Mac 全屏时自动隐藏程序坞</a>
          </li>
        
          <li>
            <a href="/hexo/2021/07/29/Mac%E7%B3%BB%E7%BB%9F%E4%B8%AD%E7%9A%84.DS_Store/">Mac系统中的.DS_Store</a>
          </li>
        
          <li>
            <a href="/hexo/2021/07/21/Markdown/">Markdown</a>
          </li>
        
          <li>
            <a href="/hexo/2021/07/11/Github%E5%9B%BD%E5%86%85%E9%95%9C%E5%83%8F%E7%BD%91%E7%AB%99%EF%BC%8C%E8%A7%A3%E5%86%B3Github%E8%AE%BF%E9%97%AE%E7%9A%84%E7%A5%9E%E5%99%A8/">Github国内镜像网站，解决Github访问的神器</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2021 John Doe<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/hexo/" class="mobile-nav-link">Home</a>
  
    <a href="/hexo/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/hexo/fancybox/jquery.fancybox.css">

  
<script src="/hexo/fancybox/jquery.fancybox.pack.js"></script>




<script src="/hexo/js/script.js"></script>




  </div>
</body>
</html>